package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.MatchBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.MatchModel;
import cst.a931xmc.yuejian20.R;

public class MatchActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int match_id;
    private ImageView match_pic;
    private TextView match_name;
    private TextView match_place;
    private TextView match_time;
    private TextView match_intro;
    private ImageButton btn_back;
    private MatchModel model;
    private Listener<MatchBean> listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);
        init();
        initData();
        model = new MatchModel();
        model.getDetail(match_id,listener);
    }

    public void init(){
        match_pic = findViewById(R.id.match_pic);
        match_name = findViewById(R.id.match_name);
        match_place = findViewById(R.id.match_place);
        match_time = findViewById(R.id.match_time);
        match_intro = findViewById(R.id.match_intro);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        Intent intent = getIntent();
        match_id = intent.getExtras().getInt("match_id");
    }

    public void initData(){
        listener = new Listener<MatchBean>() {
            @Override
            public void onResponse(MatchBean l) {
                if (l.getMatch_id()!=0){
                    match_name.setText(l.getMatch_name());
                    match_place.setText("地点：" + l.getMatch_place());
                    match_time.setText("时间：" + l.getMatch_time());
                    match_intro.setText(l.getMatch_intro());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getMatch_pic())
                            .placeholder(R.drawable.nopic)
                            .into(match_pic);
                }
                else {
                    Toast.makeText(MatchActivity.this,"加载失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(MatchActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
